package com.boang.jdbc;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.function.Consumer;

/*
  @author: 13813
  @date: 2024/12/9 14:05
*/
public class JdbcDemo01 {
    public static void main(String[] args) throws ClassNotFoundException {
//        claQuery();
//        stuQuery();
//        m3();
//        m4();
        m5("zhangsan", 20);
    }

    //设置查询参数
    private static void m5(String name, int age) {
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db62", "root", "123456");
            preparedStatement = connection.prepareStatement("select * from student where name = ? and age = ?");
            //赋值
            preparedStatement.setString(1, name);
            preparedStatement.setInt(2, age);

            resultSet = preparedStatement.executeQuery();
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String nm = resultSet.getString("name");
                Student student = new Student(id, nm);
                list.add(student);
            }
            list.stream().forEach(System.out::println);
        } catch (ClassNotFoundException e) {
        } catch (SQLException e) {
        } finally {
            try {
                resultSet.close();
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
            }
        }
    }

    //try...with...resource，把需要关闭的资源放到 try() 中
    private static void m4() throws ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        try (
                Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db62", "root", "123456");
                PreparedStatement preparedStatement = connection.prepareStatement("select * from student");
                ResultSet resultSet = preparedStatement.executeQuery();
        ) {
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Student student = new Student(id, name);
                list.add(student);
            }
            list.stream().forEach(System.out::println);
        } catch (Exception e) {
        }
    }

    //查询学生表，并把学生信息封装到Student对象中，并把Student对象添加到ArrayList集合中
    private static void m3() {
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db62", "root", "123456");
            preparedStatement = connection.prepareStatement("select * from student");
            resultSet = preparedStatement.executeQuery();
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Student student = new Student(id, name);
                list.add(student);
            }
            /*list.stream().forEach(new Consumer<Student>() {
                @Override
                public void accept(Student student) {
                    System.out.println(student);
                }
            });*/
//            list.stream().forEach(student -> System.out.println(student));
            list.stream().forEach(System.out::println);
        } catch (ClassNotFoundException e) {
        } catch (SQLException e) {
        } finally {
            try {
                resultSet.close();
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
            }
        }
    }

    //查询学生表
    private static void stuQuery() {
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db62", "root", "123456");
            preparedStatement = connection.prepareStatement("select * from student");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                int claId = resultSet.getInt("cla_id");
                String sex = resultSet.getString("sex");
                String birthday = resultSet.getString("birthday");
                BigDecimal fee = resultSet.getBigDecimal("fee");
                System.out.println(name + "," + fee);
            }
        } catch (ClassNotFoundException e) {
        } catch (SQLException e) {
        } finally {
            try {
                resultSet.close();
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
            }
        }
    }

    //查询班级表
    private static void claQuery() throws ClassNotFoundException, SQLException {
        //创建 Driver 对象 （加载驱动）
        Class.forName("com.mysql.cj.jdbc.Driver");
        new com.mysql.cj.jdbc.Driver();
        //连接数据库                                                协议         主机      端口  数据名       用户名             密码
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db62", "root", "123456");

        //预编译SQL
        PreparedStatement preparedStatement = connection.prepareStatement("select * from cla");

        //执行SQL语句
        ResultSet resultSet = preparedStatement.executeQuery();

        //遍历ResultSet
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id = " + id + ", name = " + name);
        }
    }
}
